package com.starsoft.smdc.crawler.xueqiu;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.starsoft.frame.fileimport.JsonReader;
import com.starsoft.frame.fileimport.pojo.Column;
import com.starsoft.frame.fileimport.pojo.DataTable;
import com.starsoft.frame.fileimport.pojo.NullType;
import com.starsoft.frame.fileimport.pojo.Table;
import com.starsoft.frame.fileimport.pojo.Table.TitleType;
import com.starsoft.frame.fileimport.pojo.ValueType;
import com.starsoft.frame.util.DateUtil;
import com.starsoft.frame.util.MapUtil;
import com.starsoft.frame.util.StringUtil;
import com.starsoft.smdc.bean.SmdcHandicap;
import com.starsoft.smdc.bean.SmdcMarketDaily;
import com.starsoft.smdc.bean.SmdcMarketDailyId;
import com.starsoft.smdc.bean.SmdcSecurity;
import com.starsoft.smdc.crawler.common.HandicapCrawler;
import com.starsoft.smdc.util.SecurityType;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;

@Component
public class HandicapXueqiu extends XueqiuService implements HandicapCrawler{

	private static Logger logger = LoggerFactory.getLogger(HandicapXueqiu.class);
	
	private Map<String, SmdcHandicap> handicapCache = new HashMap<>();
	// 缓存有效时间2mins
	private int validateTime = 1000*60*2;


	public HandicapXueqiu() {
		this.apiMap = new HashMap<>();
		apiMap.put("handicap", "https://stock.xueqiu.com/v5/stock/batch/quote.json?symbol=%s&extend=detail&is_delay_hk=false");
		this.setXueqiuParent("https://xueqiu.com");
	}
	
	
	@Override
	public SmdcHandicap getHandicap(SmdcSecurity security) {
		List<SmdcHandicap> handicapList=  this.getHandicap(Arrays.asList(security));
		if(handicapList.size()>0) return handicapList.get(0);
		return null;
	}
	
	@Override
	public List<SmdcHandicap> getHandicap(List<SmdcSecurity> securityList) {
		long now = new Date().getTime();
		List<SmdcHandicap> handicapList = new ArrayList<SmdcHandicap>();
		if(securityList==null || securityList.isEmpty()){
			return handicapList;
		}
		
		String secIds = "";
		for(SmdcSecurity security:securityList){
			secIds += convertSecId(security.getSecId(), false)+",";
		}

		return getHandicap(secIds);
	}
	
	private List<SmdcHandicap> getHandicap(String secIds) {
		List<SmdcHandicap> handicapList = new ArrayList<SmdcHandicap>();
		String apiName = "handicap";
		String response = this.request(apiName, secIds);
		if(StringUtil.isEmpty(response)){
			throw new RuntimeException(secIds+"xueqiu handicap no response");
		}
		List<Map<String, Object>> listValue = (List<Map<String, Object>>) JSONUtil.parse(response).getByPath("data.items.quote");
		
		List<Column> columnList = new ArrayList<Column>();
		columnList.add(new Column("symbol", "secId", ValueType.String, NullType.notnull));
		columnList.add(new Column("code", "ticker", ValueType.String, NullType.notnull));
		columnList.add(new Column("current", "price", ValueType.Double, NullType.nullable));
		columnList.add(new Column("open", "openPrice", ValueType.Double, NullType.nullable));
		columnList.add(new Column("last_close", "preClosePrice", ValueType.Double, NullType.nullable));
		columnList.add(new Column("high", "highPrice", ValueType.Double, NullType.nullable));
		columnList.add(new Column("low", "lowPrice", ValueType.Double, NullType.nullable));
		columnList.add(new Column("percent", "percent", ValueType.Double, NullType.nullable));
		columnList.add(new Column("updown", "chg", ValueType.Double, NullType.nullable));
		columnList.add(new Column("volume", "volume", ValueType.Double, NullType.nullable));
		columnList.add(new Column("amount", "turnoverValue", ValueType.Double, NullType.nullable));
		columnList.add(new Column("turnover_rate", "turnoverRate", ValueType.Double, NullType.nullable));
		
		DataTable table = new JsonReader().read(listValue, new Table(TitleType.OnlyName, columnList));
		for (Map<String, Object> mapObject : table.getData()) {
			String secId = convertSecId((String) mapObject.get("secId"), true);
			mapObject.put("secId", secId);
			SmdcHandicap handicap = MapUtil.toBean(SmdcHandicap.class, mapObject);
			if (handicap.getTurnoverVol() == 0) {
				handicap.setOpen(false);
			}
			handicap.setPercent(handicap.getPercent()/100);
			handicap.setUpdateTime(new Date());
			handicapList.add(handicap);
			handicapCache.put(secId, handicap);
		}
		return handicapList;
	}

	@Override
	public List<SmdcMarketDaily> update(SmdcSecurity security, Date begin) {
		throw new RuntimeException("暂不支持");
	}

	@Override
	public List<SmdcMarketDaily> update(List<SmdcSecurity> securityList, List<Date> begin) {
		throw new RuntimeException("暂不支持");
	}

}
